package com.springboot.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.springboot.entity.Employee;
import com.springboot.service.DeptService;
import com.springboot.service.EmployeeService;
import com.springboot.utils.R;
import com.springboot.vo.EmployeeVo;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/employee")

public class EmployeeController {
    private final EmployeeService employeeService;
    private final DeptService deptService;

    public EmployeeController(EmployeeService employeeService, DeptService deptService) {
        this.employeeService = employeeService;
        this.deptService = deptService;
    }
    @GetMapping("/getEmployee/{id}")
    public Employee getEmployee(@PathVariable Long id) {
        Employee employee = employeeService.getById(id);
        if(employee==null||employee.getIsDeleted() == 1)
            return null;
        return employee;
    }

    @PostMapping("/conditionFindByPage")
    public IPage<Employee> conditionFindByPage(Integer page, Integer pageSize, @RequestBody EmployeeVo employeeVo) {
        return employeeService.conditionFindByPage(page, pageSize, employeeVo);
    }

    @PostMapping("/addEmployee")
    public R addEmployee(@RequestBody Employee employee) {
        employee.setDeptName(deptService.getById(employee.getDeptId()).getDeptName());
        return employeeService.save(employee) ? R.ok().message("添加成功！"): R.error().message("添加失败！");
    }
    @PostMapping("/updateEmployee")
    public R updateEmployee(@RequestBody Employee employee) {
        employee.setIsDeleted(0);
        return employeeService.updateById(employee) ? R.ok().message("修改成功！"): R.error().message("修改失败！");
    }
    @PostMapping("/batchDeleteEmployee")
    public R batchDeleteEmployee(@RequestBody List<Long> ids) {
        return employeeService.removeByIds(ids) ? R.ok().message("删除成功！"): R.error().message("删除失败！");
    }

}
